package dkbta.util.plan;

import static dkbta.util.Utils.readDOMDocFromXML;

import java.io.File;
import java.util.ArrayList;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import dkbta.CoordinatorConfig;
import dkbta.ontology.loading.OntologyException;
import dkbta.processing.PlanException;
import dkbta.util.data.StatInfo;

public abstract class AbstractPlanMaker {
	
private ArrayList<StatInfo> stats;
	
public AbstractPlanMaker(Element plan) {
	try{
		stats = parseStatisticalAbstractions(plan);	
	}
	catch (Exception e){
		e.printStackTrace();
	}
}

public void makePlans(String taskFileName, int numOfUnits){
	
}

public ArrayList<StatInfo> getStats() {
	return stats;
}

@SuppressWarnings("static-access")
private ArrayList<StatInfo> parseStatisticalAbstractions(Element plan) throws Exception{
		
		Element ontologyElement = CoordinatorConfig.getXmlElement(plan, "Ontology");
		if (ontologyElement == null){
			throw new PlanException("No 'Ontology' element in the task");
		}
		
		Element statsticalNode = CoordinatorConfig.getXmlElement(plan, "Statistical");
		String ontologyFileName = ontologyElement.getAttribute("file");
		Element statisticalOntologyNode = CoordinatorConfig.getXmlElement(readDOMDocFromXML(new File(ontologyFileName)).getDocumentElement(), "Stats");
		
		if (statsticalNode == null || statisticalOntologyNode == null){
			return null; // No statistical abstractions
		}
		
		ArrayList<StatInfo> statsInfo = new ArrayList<StatInfo>();
		NodeList statNodes = statsticalNode.getChildNodes();
		NodeList statOntologyNodes = statisticalOntologyNode.getChildNodes();
		
		int length = statNodes.getLength();
		int ontologyLength = statOntologyNodes.getLength();
		
		for (int i = 0; i < length; ++i){
			Node node = statNodes.item(i);
			if (node.getNodeType() != Node.ELEMENT_NODE){
				continue;
			}
			Element statElement = (Element) statNodes.item(i);
			String name = statElement.getAttribute("name");
			String patients = statElement.getAttribute("patients");
			String sendToDB = statElement.getAttribute("sendToDB");
			
			for(int j = 0; j < ontologyLength; j++){
				Node ontologyNode = statOntologyNodes.item(j);
				if (ontologyNode.getNodeType() != ontologyNode.ELEMENT_NODE){
					continue;
				}
				Element statOntologyElement = (Element)statOntologyNodes.item(j);
				String ontologyName = statOntologyElement.getAttribute("name");
				if(name.equals(ontologyName)){
					Element afsElement = CoordinatorConfig.getXmlElement(statOntologyElement, "AbstractedFrom");
					if (afsElement == null){
						throw new OntologyException("No abstracted-from element");
					}

					Element afElement = getAfElement(afsElement);
					String abstractedFromName = afElement.getAttribute("name");
					boolean isPrimitive = afElement.getNodeName().equals("Primitive");
					statsInfo.add(new StatInfo(ontologyName, patients, sendToDB, abstractedFromName, isPrimitive));
					break;
				}
			}
			
		}
		return statsInfo;
}
	
private Element getAfElement(Element afsElement) throws OntologyException{
	NodeList childNodes = afsElement.getChildNodes();

	int elementNodeCount = 0;
	Element afElement = null;
	for (int i = 0; i < childNodes.getLength(); ++i){
		Node node = childNodes.item(i);
		if (node.getNodeType() == Node.ELEMENT_NODE){
			elementNodeCount++;
			afElement = (Element)node;
		}
	}

	if (elementNodeCount != 1){
		throw new OntologyException(
				"A statistical abstraction can be abstracted from only one element and not "
						+ elementNodeCount);
	}
	return afElement;
}



}
